package com.ruoyi.system.service.impl;

import com.ruoyi.common.utils.excel.ImportExcelUtil;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.mapper.WarehouseMapper;
import com.ruoyi.system.service.IWarehouseService;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 仓库Service业务层处理
 *
 * @author ruoyi
 * @date 2020-12-16
 */
@Service
public class WarehouseServiceImpl implements IWarehouseService {
    @Resource
    private WarehouseMapper warehouseMapper;

    /**
     * 查询仓库
     *
     * @param wId 仓库ID
     * @return 仓库
     */
    @Override
    public Warehouse selectWarehouseById(Long wId) {
        return warehouseMapper.selectWarehouseById(wId);
    }

    /**
     * 查询仓库列表
     *
     * @param warehouse 仓库
     * @return 仓库
     */
    @Override
    public List<Warehouse> selectWarehouseList(Warehouse warehouse) {
        List<Warehouse> list = warehouseMapper.selectWarehouseList(warehouse);
        //计算总金额

        for (int i = 0; i < list.size(); i++) {
            List<Materialdetails> Materialdetail = warehouseMapper.querymoney(list.get(i).getwId());
            double money = 0.0;
            for (int j = 0; j < Materialdetail.size(); j++) {
                BigDecimal b1 = new BigDecimal(Materialdetail.get(j).getMdPrice() + "");
                BigDecimal b2 = new BigDecimal(Materialdetail.get(j).getMdSum() + "");
                money += b1.multiply(b2).doubleValue();
            }
            DecimalFormat df = new DecimalFormat("######0.00");
            list.get(i).setMoney(Double.valueOf(df.format(money)));
        }

        return list;
    }

    @Override
    public List<Warehouse> quryWarehouseList() {
        return warehouseMapper.quryWarehouseList();
    }

    /**
     * 查询仓库操作明细列表
     *
     * @return 仓库集合
     */
    public List<MaterialFour> warehouserecordlist(HashMap<String, Object> map) {
        List<MaterialFour> list = warehouseMapper.warehouserecordlist(map);
        for (int i = 0; i < list.size(); i++) {
            double money = 0.0;
            DecimalFormat df = new DecimalFormat("######0.00");
            BigDecimal b1 = new BigDecimal(Double.toString(list.get(i).getMdPrice()));
            BigDecimal b2 = new BigDecimal(Double.toString(list.get(i).getWrNumber()));
            money += b1.multiply(b2).doubleValue();
            list.get(i).setMdMoney(Double.valueOf(df.format(money)));
        }
        return list;
    }

    /**
     * 新增仓库
     *
     * @param warehouse 仓库
     * @return 结果
     */
    @Override
    public int insertWarehouse(Warehouse warehouse) {
        if (warehouseMapper.selectWarehouseCount(warehouse.getwName()) < 1) {
            return warehouseMapper.insertWarehouse(warehouse);
        } else {
            return 0;
        }
    }

    /**
     * 修改仓库
     *
     * @param warehouse 仓库
     * @return 结果
     */
    @Override
    public int updateWarehouse(Warehouse warehouse) {
        return warehouseMapper.updateWarehouse(warehouse);
    }

    /**
     * 删除仓库信息
     *
     * @param ids 仓库ID
     * @return 结果
     */
    @Override
    public int deleteWarehouseById(Long ids) {
        int a = warehouseMapper.selectMateialCount(ids);
        if (a < 1) {
            return warehouseMapper.deleteWarehouseById(ids);
        }
        return 0;
    }

    @Override
    public List<Materialdetails> querymoney(Long wid) {

        return warehouseMapper.querymoney(wid);
    }

    @Override
    public List<MaterialTwo> selectMaterialList(Material material, Integer wid) {
        HashMap<String, Object> map = new HashMap();
        map.put("material", material);
        map.put("wid", wid);
        return warehouseMapper.selectMaterialList(map);
    }

    @Override
    public int updateMdType(MultipartFile file) {
        String fileName = file.getName();
        List<String[]> dataList = null;
        String headData = null;
        Workbook workbook = null;
        String dateFormat = "yyyy-MM-dd";
        boolean t = true;
        int a = 0;
        try {
            //部门
            workbook = new XSSFWorkbook(file.getInputStream());
            if (workbook != null) {
                headData = ImportExcelUtil.getExcelHeadData(workbook);
                dataList = ImportExcelUtil.getExcelData(workbook, 1, 2, dateFormat);
            }
            //拿到数据进行数据库操作
            Map map = new HashMap();
            for (int j = 1; j < dataList.size(); j++) {
                HashMap map1= new HashMap();
                String[] strings = dataList.get(j);
                for (int i = 0; i < 8; i++) {
                    String cellValue = dataList.get(j)[i] + "";
                    if (!"null".equals(cellValue)) {
                        t = true;
                        switch (i) {
                            case 1:
                                map1.put("m_name",cellValue);
                                break;
                            case 2:
                                map1.put("m_model",cellValue);
                                break;
                            case 3:
                                map1.put("md_specifications",cellValue);
                                break;
                            case 6:
                                map1.put("md_price",cellValue);
                                break;
                            case 7:
                                int type=0;
                                if(cellValue.equals("乙购"))type=1;
                                if(cellValue.equals("甲供"))type=0;
                                map1.put("md_type",type);
                                break;
                            default:
                                break;
                        }

                    }
                }
                warehouseMapper.updateMdType(map1);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
        }
        return 1;
    }
}